/**
 * @param {number[]} height
 * @return {number}
 */
var maxArea = function (height) {
  if (height.length < 2) return 0
  let left = 0,
    right = height.length - 1
  let leftMax = height[left],
    rightMax = height[right]

  let maxVal = 0
  while (left < right) {
    // 找出最小值计算面积
    let minVal = Math.min(leftMax, rightMax)
    // 判断计算的值如果比之前大则更新
    if (minVal * (right - left) > maxVal) {
      maxVal = minVal * (right - left)
    }
    if (height[left] <= height[right]) {
      left++
      leftMax = height[left]
    } else if (height[left] > height[right]) {
      right--
      rightMax = height[right]
    }
  }
  return maxVal
}



var maxArea1 = function (height) {
  if (height.length < 2) return 0
  let left = 0,
    right = height.length - 1
  let leftMax = height[left],
    rightMax = height[right]

  let maxVal = 0
  while (left !== right) {
    // 找出最小值计算面积
    let minVal = Math.min(leftMax, rightMax)
    // 判断计算的值如果比之前大则更新
    if (minVal * (right - left) > maxVal) {
      maxVal = minVal * (right - left)
    }
    if (height[left] < height[right]) {
      left++
      leftMax = height[left]
    } else if (height[left] >= height[right]) {
      right--
      rightMax = height[right]
    }
  }
  return maxVal
}
const res = maxArea([1, 8, 6, 2, 5, 4, 8, 3, 7])
console.log(res)
